package v9.assist.script


// import v9.assist.access.btnAtvConfirm

import sample.ui.AppDispatchers
import sample.ui.Properties
import sample.ui.ConsoleLayout
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.C
import kotlinx.coroutines.script.Controller
import kotlinx.coroutines.script.Controller.Companion.processExitGame
import kotlinx.coroutines.script.accAutoAtk
import kotlinx.coroutines.script.tes.DiskLogUtils
import kotlinx.coroutines.script.tes.appScopeLaunch
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.pt
import kotlinx.coroutines.script.tes.random
import kotlinx.coroutines.script.tes.readByDisk
import kotlinx.coroutines.script.tes.shake
import kotlinx.coroutines.script.tes.toast
import v9.assist.access.accClick
import v9.assist.access.accPathfinding
import v9.assist.access.btnApplyAccept
import v9.assist.access.btnApplyMenu
import v9.assist.access.btnApplyPopMenu
import v9.assist.access.btnApplyRefuse
import v9.assist.access.btnCloseable
import v9.assist.access.btnEditFriendClose
import v9.assist.access.btnInteract
import v9.assist.access.btnInviteBlackClose
import v9.assist.access.btnInviteCancel
import v9.assist.access.btnInviteConfirm
import v9.assist.access.btnPopApplyClose
import v9.assist.access.btnPopTxtCancel
import v9.assist.access.btnPopTxtConfirm
import v9.assist.access.btnRandomBuff
import v9.assist.access.btnReviveByOtherCancel
import v9.assist.access.btnReviveInCity
import v9.assist.access.btnReviveInHere
import v9.assist.access.btnSceneInviteGroupAllClose
import v9.assist.access.btnScreenCenter
import v9.assist.access.btnSelectRole
import v9.assist.access.btnSkipAnim
import v9.assist.access.releaseLastGestures
import v9.assist.analyst.MODE_PATHFINDING_P1
import v9.assist.analyst.MODE_PATHFINDING_P2
import v9.assist.analyst.MODE_PATHFINDING_P4
import v9.assist.analyst.MODE_PATHFINDING_P5
import v9.assist.analyst.MODE_PATHFINDING_P6
import v9.assist.analyst.PAGE_APPLY
import v9.assist.analyst.PAGE_APPLY_EMPTY
import v9.assist.analyst.PAGE_BASE_MENU
import v9.assist.analyst.PAGE_BE_INVITED
import v9.assist.analyst.PAGE_BE_INVITED_BLACK_GROUP
import v9.assist.analyst.PAGE_BOSS_DIRECT_OR_BUFF
import v9.assist.analyst.PAGE_CLOSEABLE
import v9.assist.analyst.PAGE_EDIT_FRIEND_NAME
import v9.assist.analyst.PAGE_INITIAL
import v9.assist.analyst.PAGE_MAIN
import v9.assist.analyst.PAGE_POP_TEXT
import v9.assist.analyst.PAGE_REVIVE
import v9.assist.analyst.PAGE_REVIVE_BY_OTHER
import v9.assist.analyst.PAGE_ROOT_ENTER
import v9.assist.analyst.PAGE_ROOT_ENTER_TIP
import v9.assist.analyst.PAGE_SCENE_COMPLETED
import v9.assist.analyst.PAGE_SCENE_ENTER
import v9.assist.analyst.PAGE_SCENE_GROUP_INVITE_PLAYER
import v9.assist.analyst.PAGE_SELECT_ROLE
import v9.assist.analyst.PAGE_SKIP
import v9.assist.analyst.PAGE_TALK_BLACKSMITH
import v9.assist.analyst.PAGE_TALK_NPC
import v9.assist.analyst.anaClearSceneSettle
import v9.assist.analyst.anaLastHasBackpack100Ts
import v9.assist.analyst.anaLastHasEnemyNameTs
import v9.assist.analyst.anaLastHasInteractTs
import v9.assist.analyst.anaLastSMapFMFeature10Ts
import v9.assist.analyst.anaLastSMapFMFeature11Ts
import v9.assist.analyst.anaLastSMapFMFeature12Ts
import v9.assist.analyst.anaLastSMapFMFeature13Ts
import v9.assist.analyst.anaLastSMapFMFeature14Ts
import v9.assist.analyst.anaLastSMapFMFeature15Ts
import v9.assist.analyst.anaLastSMapFMFeature16Ts
import v9.assist.analyst.anaLastSMapFMFeature17Ts
import v9.assist.analyst.anaLastSMapFMFeature18Ts
import v9.assist.analyst.anaLastSMapFMFeature19Ts
import v9.assist.analyst.anaLastSMapFMFeature1Ts
import v9.assist.analyst.anaLastSMapFMFeature20Ts
import v9.assist.analyst.anaLastSMapFMFeature2Ts
import v9.assist.analyst.anaLastSMapFMFeature3Ts
import v9.assist.analyst.anaLastSMapFMFeature4Ts
import v9.assist.analyst.anaLastSMapFMFeature5Ts
import v9.assist.analyst.anaLastSMapFMFeature6Ts
import v9.assist.analyst.anaLastSMapFMFeature7Ts
import v9.assist.analyst.anaLastSMapFMFeature8Ts
import v9.assist.analyst.anaLastSMapFMFeature9Ts
import v9.assist.analyst.anaLastSceneSettleTs
import v9.assist.analyst.anaPathfindingModeFM
import v9.assist.analyst.analystInitialized
import v9.assist.analyst.angleByRoad
import v9.assist.analyst.blockBlacksmith
import v9.assist.analyst.blockBlueDoor
import v9.assist.analyst.blockEliteEnemyBoss
import v9.assist.analyst.bsAcceptApply
import v9.assist.analyst.bsAcceptInvite
import v9.assist.analyst.bsBossBeside
import v9.assist.analyst.bsEnemy
import v9.assist.analyst.bsHasApplyDot
import v9.assist.analyst.bsHasEnemy
import v9.assist.analyst.bsHasInteract
import v9.assist.analyst.bsHasReviveRightBtn
import v9.assist.analyst.bsHasTransmitting
import v9.assist.analyst.bsPage
import v9.assist.analyst.bsPlayerHpLosing
import v9.assist.analyst.bsSceneSettle
import v9.assist.analyst.bsSceneSettleBlacksmith
import v9.assist.analyst.counterActionStopped
import v9.assist.analyst.fsInfo

import v9.assist.analyst.lastEliteEnemyBossTs
import v9.assist.analyst.setupAnalystScene
import kotlin.math.max

private var scriptInError = false
private var n: Long? = null // 检查CDK用的时间戳标记
private var job: Job? = null

private var sLastFeature1Ts = 0L
private var sLastFeature2Ts = 0L
private var sLastFeature3Ts = 0L //
private var sLastFeature4Ts = 0L // 直通
private var sLastFeature5Ts = 0L //
private var sLastFeature6Ts = 0L //
private var sLastFeature7Ts = 0L // 1层Boss击破去新场景
private var sLastFeature8Ts = 0L //
private var sLastFeature9Ts = 0L //
private var sLastFeature10Ts = 0L
private var sLastFeature11Ts = 0L //死路起点
private var sLastFeature12Ts = 0L // P4死路
private var sLastFeature13Ts = 0L
private var sLastFeature14Ts = 0L
private var sLastFeature15Ts = 0L
private var sLastFeature16Ts = 0L
private var sLastFeature17Ts = 0L // 副本起点
private var sLastFeature18Ts = 0L
private var sLastFeature19Ts = 0L
private var sLastFeature20Ts = 0L // p6起点

fun startSceneFM() {
    job?.cancel()
    job = appScopeLaunch(AppDispatchers.Script) {
        scriptInError = false
        xCounterNetRetry = Properties.netRetryCount
        xLastAtvTs = System.currentTimeMillis()
        xLastSceneEnterTs = System.currentTimeMillis() // 现在只记录副本开始时间
        setupAnalystScene(C.S_FM)
        n = System.currentTimeMillis()
        delay(200) 
        while(Controller.isRunning && !scriptInError) {
            checkScriptError()
            if (!analystInitialized) {
                printLog { "Script invoking not ready" }
                ConsoleLayout.outputText("等待首帧截屏.连续出现重启脚本")
                toast("等待首帧截屏.连续出现重启脚本")
                delay(1000) // 刚开始没准备好
            } else {
                val timestamp = System.currentTimeMillis()
                processFsFM()
                val using = System.currentTimeMillis() - timestamp
                if (using < 10) delay(if (bsPage == PAGE_INITIAL) 800L.shake(50) else 200L.shake(5))
            }
        }
    }
}

fun stopSceneFM() {
    xBossDirectOrBuff = false
    xLastAtvTs = 0 // 不能少
    job?.cancel()
    anaClearSceneSettle()
}

suspend fun processFsFM() {
    fsInfo.also { if (bsPage != SCRIPT_MAIN) printLog { "processFs $anaPathfindingModeFM $anaPathfindingModeFM $anaPathfindingModeFM $anaPathfindingModeFM $it" } }

    if (bsPage == PAGE_BE_INVITED) {
        val widget = if (bsAcceptInvite) btnInviteConfirm else btnInviteCancel
        accClick(widget, afterDelay = 500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
    } else if (bsPage == PAGE_REVIVE) {
        if (bsHasReviveRightBtn) {
            accClick(btnReviveInHere, afterDelay = 333)
        } else {
            accClick(btnReviveInCity, afterDelay = 333)
            if (xBossDirectOrBuff) xLastBossDirectOrBuffTs = System.currentTimeMillis()
            if (anaPathfindingModeFM != MODE_PATHFINDING_P5 && anaPathfindingModeFM != MODE_PATHFINDING_P6) {
                printLog { "scriptFM. 推进中死亡.切换到p1" }
                anaPathfindingModeFM = MODE_PATHFINDING_P1
            }
        }
        xLastAtvTs = System.currentTimeMillis()
        xLastReviveTs = System.currentTimeMillis()
    } else if (bsPage == PAGE_SCENE_ENTER) {
        accSceneEnter()
        anaPathfindingModeFM = MODE_PATHFINDING_P1
    } else if (bsPage == PAGE_BASE_MENU) {
        accClick(btnScreenCenter, afterDelay = 2000)
    } else if (bsPage == PAGE_SKIP) {
        delay(500)
        accClick(btnSkipAnim, afterDelay = 2500)
        listOf(316, 316, 316, 316).forEachIndexed { index, i ->
            printLog { "script 画面跳过 $index" }
            accPathfinding(i)
        }
        releaseLastGestures()
        xLastAtvTs = System.currentTimeMillis()
    } else if (bsPage == PAGE_POP_TEXT) {
        accClick(btnPopTxtCancel, afterDelay = 2400)
        val aa = if (anaPathfindingModeFM == MODE_PATHFINDING_P5 || anaPathfindingModeFM == MODE_PATHFINDING_P6) 70 else 320
        listOf(aa, aa, aa, aa, aa, aa, aa).forEachIndexed { index, i ->
            printLog { "script 防止卡地图移动 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        accClick(btnInteract, afterDelay = 1500)
        accClick(btnPopTxtConfirm, afterDelay = 2000) // 如果是在外面的入口卡住
        xLastAtvTs = System.currentTimeMillis() // 延长时间
    } else if (bsPage == PAGE_BE_INVITED_BLACK_GROUP) {
        delay(500)
        accClick(btnInviteBlackClose, afterDelay = 1500)
    } else if (bsPage == PAGE_EDIT_FRIEND_NAME) {
        delay(500)
        accClick(btnEditFriendClose, afterDelay = 1500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
    } else if (bsPage == PAGE_SCENE_GROUP_INVITE_PLAYER) {
        delay(500)
        accClick(btnSceneInviteGroupAllClose, afterDelay = 1500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
    } else if (bsPage == PAGE_TALK_BLACKSMITH || bsPage == PAGE_SCENE_COMPLETED
        || bsSceneSettleBlacksmith || bsSceneSettle
    ) {
        if (bsEnemy == "Y" || bsEnemy == "B" || bsEnemy == "P" || blockEliteEnemyBoss != null
            && System.currentTimeMillis() - sLastFeature34Ts > 1000) {
            anaClearSceneSettle()
            sLastFeature34Ts = System.currentTimeMillis()
        }
        else {
            xLastAtvTs = System.currentTimeMillis()
            sceneHandlingSettle(
                blacksmithBy = { blacksmithByBlueDoor(blockBlueDoor) ?: blacksmithByBlacksmith(blockBlacksmith) },
                exitBy = { exitByBlueDoor(blockBlueDoor) ?: exitByBlacksmith(blockBlacksmith) }
            )
        }
    } else if (bsPage == PAGE_TALK_NPC) {
        delay(500)
        accClick(btnScreenCenter, afterDelay = 1500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        } // 有这段
    } else if (bsPage == PAGE_APPLY_EMPTY) {
        accClick(btnPopApplyClose, afterDelay = 1000)
    } else if (bsPage == PAGE_APPLY) {
        accClick(if (bsAcceptApply) btnApplyAccept else btnApplyRefuse, afterDelay = 1000)
    } else if (bsPage == PAGE_SELECT_ROLE) {
        accClick(btnSelectRole, afterDelay = 1500)
    } else if (bsPage == PAGE_CLOSEABLE) {
        delay(500)
        accClick(btnCloseable, afterDelay = 3000)
        xLastCloseableTs = System.currentTimeMillis()
    } else if (bsPage == PAGE_BOSS_DIRECT_OR_BUFF) {
        delay(500)
        accClick(btnRandomBuff, afterDelay = 2000)
        xLastBossDirectOrBuffTs = System.currentTimeMillis()
        xBossDirectOrBuff = true
    } else if (bsPage == PAGE_REVIVE_BY_OTHER) {
        accClick(btnReviveByOtherCancel, afterDelay = 1500)
    } else if (bsPage == PAGE_ROOT_ENTER_TIP || bsPage == PAGE_ROOT_ENTER) {
        tryEnterByRelaunch()
    } else if (bsPage == PAGE_MAIN) { // 主页操作
        if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) {
            accAutoAtk(angleByRoad)
            xLastAtvTs = System.currentTimeMillis()
            if (bsHasEnemy) xLastAtkTs = System.currentTimeMillis()
        } else if (
            (System.currentTimeMillis() - xLastBossDirectOrBuffTs < 30000 || (System.currentTimeMillis() - xLastReviveTs < 30000 && xBossDirectOrBuff))
            && System.currentTimeMillis() - anaLastHasEnemyNameTs > 3000
            && blockBlueDoor != null) {
            listOf(50, 55, 50, 55, 50, 55, 50, 60, 55, 270).forEachIndexed { index, i ->
                printLog { "scriptFM 节点.直通直接去bossA $index" }
                accPathfinding(i, if (index >= 7) btnInteract else null)
            }
            accClick(btnInteract, afterDelay = 500)
        } else if (bsHasTransmitting
            && !bsPlayerHpLosing
            && System.currentTimeMillis() - anaLastHasInteractTs < 9000) {
            // 开门流程
            delay(500)
        } else if (System.currentTimeMillis() - anaLastHasBackpack100Ts < 50000
            && (System.currentTimeMillis() - xLastAtkTs > 3000 || System.currentTimeMillis() - anaLastSceneSettleTs < 10000)
            && System.currentTimeMillis() - sLastFeature29Ts > 50000) {
            printLog { "script 满背包尝试分解" }
            tryPetBlacksmithSubmit()
            sLastFeature29Ts = System.currentTimeMillis()
        } else if (bsHasInteract && !bsPlayerHpLosing
            && System.currentTimeMillis() - xLastUnknownTs > 8000
            && System.currentTimeMillis() - xLastSceneEnterTs > 12000
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 16000  // 击杀boss后不要乱点
            && System.currentTimeMillis() - xLastBossDirectOrBuffTs > 20000
            && !bsSceneSettle && !bsSceneSettleBlacksmith
            && System.currentTimeMillis() - anaLastSceneSettleTs > 15000
            && System.currentTimeMillis() - xLastReviveTs > 15000
            && System.currentTimeMillis() - xLastCloseableTs > 10000) {
            accClick(btnInteract, afterDelay = 0)
            accPathfinding(angleByRoad)
            accPathfinding(angleByRoad)
            accPathfinding(random.nextInt(360))
        } else if (bsHasApplyDot && Properties.inviteAutoAccept != 0) {
            accClick(btnApplyMenu, afterDelay = 500)
            accClick(btnApplyPopMenu, afterDelay = 1000)
        } else {
            // 寻路流程
            sceneFMSpecialHandling1()
            accPathfinding(sceneAngleByRoad(), pathfindingWith())
        }
    } else if (bsPage != PAGE_MAIN) {
        scriptHandlingUnknown()
    }
}


private fun pathfindingWith(): String? {
//    if (!bsSceneSettle && !bsSceneSettleBlacksmith
//        && System.currentTimeMillis() - anaLastSceneSettleTs > 20000
//        && System.currentTimeMillis() - xLastSceneEnterTs > 10000
//    ) { // 目前不需要全程互动.用精准的节点判断
//    }
    return null
}


private suspend fun sceneFMSpecialHandling1() {
    if (System.currentTimeMillis() - xLastAtkTs > 240000
        && System.currentTimeMillis() - xLastAtvTs > 240000 // 脚本开启后
        && System.currentTimeMillis() - xLastSceneExitTs > 120000) {
        printLog { "scriptFM 节点.超时退出" }
        sceneExit()
        anaPathfindingModeFM = MODE_PATHFINDING_P1 // 重制
    } else if (counterActionStopped > 30 && System.currentTimeMillis() - xLastAtkTs > 3000
        && System.currentTimeMillis() - sLastFeature33Ts > 60000) {
        printLog { "script 节点.原地不动." }
        releaseLastGestures()
        counterActionStopped = 0
        sLastFeature33Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature1Ts < 2000
        && System.currentTimeMillis() - sLastFeature1Ts > 4000) {
        listOf(330, 320, 330, 320, 330, 320, 330, 320, 330, 320, 330, 320).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.副本外入口 $index" } // 遇到门就互动
            accPathfinding(i, if (index >= 4) btnInteract else null)
        }
        accClick(btnPopTxtConfirm, afterDelay = 2000) // 如果是在外面的入口卡住
        sLastFeature1Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature2Ts < 2000
        && System.currentTimeMillis() - sLastFeature2Ts > 4000) {
        listOf(170, 170, 170, 190, 190).forEachIndexed { index, i ->
            printLog { "scriptFM P1顶端回头清理1阶段怪物 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) {
                accAutoAtk(i)
                xLastAtkTs = System.currentTimeMillis() // 这里需要补充一个时间统计
            }
            else accPathfinding(i)
        }
        if (System.currentTimeMillis() - xLastAtkTs > 4000) {
            printLog { "scriptFM. P1战斗超时.切换为P2" }
            anaPathfindingModeFM = MODE_PATHFINDING_P2
        }
        sLastFeature2Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature3Ts < 2000
        && System.currentTimeMillis() - sLastFeature3Ts > 3000) {
        listOf(150, 150, 130, 130).forEachIndexed { index, i ->
            printLog { "scriptFM P3顶端回头清理2阶段怪物 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) {
                accAutoAtk(i)
                xLastAtkTs = System.currentTimeMillis()
            }
            else accPathfinding(i)
        }
        if (System.currentTimeMillis() - xLastAtkTs > 4000) {
            printLog { "scriptFM. P3战斗超时.切换为P4" }
            anaPathfindingModeFM = MODE_PATHFINDING_P4
        }
        sLastFeature3Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature4Ts < 2000
        && System.currentTimeMillis() - sLastFeature4Ts > 6000) {
        listOf(180, 90, 130).forEachIndexed { index, i ->
            printLog { "scriptFM 直通 $index" }
            accPathfinding(i, btnInteract)
        }
        sLastFeature4Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature5Ts < 2000
        && System.currentTimeMillis() - sLastFeature5Ts > 6000) {
        listOf(30, 40, 90, 130).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P2起点卡点往右走一步再往下 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature5Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature6Ts < 2000
        && System.currentTimeMillis() - sLastFeature6Ts > 6000) {
        listOf(0, 0, 0, 0, 0, 10, 20, 30).forEachIndexed { index, i ->
            printLog { "scriptFM P2到底了.切换去右侧房间清理节点怪 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature6Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature7Ts < 2000
        && System.currentTimeMillis() - sLastFeature7Ts > 6000) {
        listOf(90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 0).forEachIndexed { index, i ->
            printLog { "scriptFM P5 1层Boss击破去新场景 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i, if (index >= 8) btnInteract else null)
        }
        sLastFeature7Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature8Ts < 2000
        && System.currentTimeMillis() - sLastFeature8Ts > 8000) { // 也有可能是最最后.频率低点.没区分
        listOf(270, 270, 270, 270, 270, 270).forEachIndexed { index, i ->
            printLog { "scriptFM P6 电梯清理敌人 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature8Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature9Ts < 2000
        && System.currentTimeMillis() - sLastFeature9Ts > 6000) { //
        listOf(250, 260, 250, 260, 250, 260,
            250, 260, 250, 260, 250, 260,
            275, 265, 275, 265, 275, 265,).forEachIndexed { index, i ->
            printLog { "scriptFM P1 去顶部脱离小卡路往左 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature9Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature10Ts < 2000
        && System.currentTimeMillis() - sLastFeature10Ts > 6000) { //
        listOf(170, 160, 170, 190, 200).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P4回副本起点往左走第一个小卡角 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature10Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature11Ts < 2000
        && System.currentTimeMillis() - sLastFeature11Ts > 6000) { //
        listOf(170, 160, 150, 170, 160, 150, 220).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P4回副本起点往左走第二个卡角 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature11Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature12Ts < 2000
        && System.currentTimeMillis() - sLastFeature12Ts > 6000) {
        listOf(190, 180, 150, 80, 90).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P5开门的上侧 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i, if (index >= 4) btnInteract else null)
        }
        sLastFeature12Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature13Ts < 2000
        && System.currentTimeMillis() - sLastFeature13Ts > 6000) {
        listOf(270, 270, 300, 330, 10).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P5开门的下侧 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i, if (index >= 4) btnInteract else null)
        }
        sLastFeature13Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature14Ts < 2000
        && System.currentTimeMillis() - sLastFeature14Ts > 6000) {
        listOf(45, 45,).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P5开门门前 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i, if (index >= 0) btnInteract else null)
        }
        sLastFeature14Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature15Ts < 2000
        && System.currentTimeMillis() - sLastFeature15Ts > 6000) {
        listOf(10, 20, 10, 20, 10, 20, 10, 20, 10, 20, 10, 20, 10, 20, 10, 20,).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P4走错到最下面的地方 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature15Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature16Ts < 2000
        && System.currentTimeMillis() - sLastFeature16Ts > 6000) {
        listOf(90, 90, 90, 90, 90, 90, 90, 0, 0, 0, 0).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P4走错死胡同 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature16Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature17Ts < 2000
        && System.currentTimeMillis() - sLastFeature17Ts > 6000) {
        releaseLastGestures()
        listOf(45, 45).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.副本起点 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        releaseLastGestures() // 避免卡摇杆
        sLastFeature17Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature20Ts < 2000
        && System.currentTimeMillis() - sLastFeature20Ts > 6000) {
        releaseLastGestures()
        listOf(90, 90).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P6起点 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        releaseLastGestures() // 避免卡摇杆
        sLastFeature20Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature18Ts < 2000
        && System.currentTimeMillis() - sLastFeature18Ts > 6000) {
        listOf(180, 45).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P6 电梯传送口 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i, btnInteract)
        }
        sLastFeature18Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapFMFeature19Ts < 2000
        && System.currentTimeMillis() - sLastFeature19Ts > 6000) {
        listOf(140).forEachIndexed { index, i ->
            printLog { "scriptFM 节点.P6 复活电梯去boss $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i, btnInteract)
        }
        sLastFeature19Ts = System.currentTimeMillis()
    }


}

private fun blacksmithByBlueDoor(blueDoor: Block?): Block? {
    if (blueDoor == null) return null
    return Block("铁匠", blueDoor.x + 28.pt, blueDoor.y + 22.pt, 0, 0)
}
private fun blacksmithByBlacksmith(blacksmith: Block?): Block? {
    if (blacksmith == null) return null
    return Block("铁匠fixed", blacksmith.x + 2.pt, blacksmith.y + 2.pt, 0, 0)
}
private fun exitByBlueDoor(blueDoor: Block?): Block? {
    if (blueDoor == null) return null
    return Block("小地图传送门fixed", blueDoor.x - 2.pt, blueDoor.y - 2.pt, 0, 0)
}
private fun exitByBlacksmith(blacksmith: Block?): Block? {
    if (blacksmith == null) return null
    return Block("小地图传送门By铁匠", blacksmith.x - 28.pt, blacksmith.y - 22.pt, 0, 0)
}

private suspend fun checkScriptError() {
    if (xLastAtvTs == 0L) {
        return
    }
    if (xScriptEndTs > 0 && System.currentTimeMillis() > xScriptEndTs) {
        scriptInError = true
        processExitGame("定时结束(${Properties.stopCountdown}秒)")
        DiskLogUtils.saveToDisk()
    }
    val timeout = Properties.scriptStopTimeout * 1000L   // 超过分钟没有敌人停止脚本
    xAtvLeftTime = max(0, timeout - (System.currentTimeMillis() - xLastAtvTs))
    if (System.currentTimeMillis() - xLastAtvTs > timeout) {
        scriptInError = true
        processExitGame("${C.ERROR_END}=${timeout / 1000}")
        DiskLogUtils.saveToDisk()
    }
    val ne = n
    if (ne != null && System.currentTimeMillis() - ne > timeout
        && System.currentTimeMillis() - lastEliteEnemyBossTs > timeout) {
        // 还有一种情况. 就是已经运行了x分钟了.但是没有boss 可能卡遗迹了
        scriptInError = true
        processExitGame("${C.ERROR_END}=BossLoss")
        DiskLogUtils.saveToDisk()
    }
    if (ne != null && System.currentTimeMillis() - ne > 198123L) {
        val sp = "j#s#l#t#z#m#p#e#r#a".split("#")
        var pass = true
        sp.forEach {
            if (pass && readByDisk(it).isNullOrEmpty()) pass = false
        }
        printLog { "j#s#l#t#z#m#p#e#r#a [pass:$pass]" }
        if (!pass) scriptInError = true
        else n = null
    }
}